package com.ltmonitor.web.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ltmonitor.entity.Department;
import com.ltmonitor.vo.PaginateResult;
import com.ltmonitor.web.util.JsonMessage;
import com.ltmonitor.web.util.OnlineUser;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/alarmReport")
public class AlarmReport extends PaginateAction {
		//定义mylog
		protected Logger mylog = Logger.getLogger(this.getClass());
		
		
		
		/**
		 * <p>根据用户基本信息获取部门查询条件</p>
		 * <p>首先根据部门编号depId获取部门链表</p>
		 * <p>如果部门编号depId为空，则通过userId查询用户权限下所有部门</p>
		 * <p>把获取的数据放入map中，key为"depIdList"</p>
		 * @param user 当前用户
		 * @param map 查询条件集合
		 */
		private void getBasicQueryData(OnlineUser user, Map map) {
			if (user != null) {
				// 获取用户id
				long userId = user.getEntityId();
				map.put("userId", userId);
				// 根据用户id获取部门列表
				Integer depId = (Integer) map.get("depId");
				if (depId == null)
					depId = 0;

				if (depId > 0) {
					List<Long> depIdList = super.getDepartmentService().getDepIdList(depId);
					map.put("depIdList", depIdList);
					map.remove("depId");
				} else {
					// 如果部门编号为空，查询所有，那么通过userId获取部门编号
					if (userId > 0) {
						List<Department> list = super.getDepartmentService().getDepartmentListByUserId(userId);
						if (list.size() > 0) {
							List<Long> depIdList2 = new ArrayList<Long>();
							for (Department temp : list) {
								depIdList2.add(temp.getEntityId());
							}
							map.put("depIdList", depIdList2);
							map.remove("depId");
						}
					}
				} // end depId
			} // end user
		}	
		
		
		
		/**
		 * 获取查询结果，把查询封装为一个接口，输入查询语句和参数，自动输出结果
		 * @param queryId 查询语句
		 * @param jsonStr 查询参数 ，json格式
		 * @return
		 */
		private JsonMessage getResultDatas(String queryId,String jsonStr) {
			JsonMessage result = null;
			PaginateResult dataList = new PaginateResult();
			try {
				// 用于获取用户权限信息
				OnlineUser user = super.getOnlineUser();
				if (user != null) {
					// 解析上传数据
					Map<String, Object> map = JSONObject.fromObject(jsonStr);
					// 分页查询转换
					Integer pageNum = (Integer) map.get("pageNumber");
					Integer pageSize = (Integer) map.get("pageSize");
					if (pageNum == null)
						pageNum = 1;
					if (pageSize == null)
						pageSize = 15;

					// 获取权限获取基本的查询条件
					getBasicQueryData(user, map);
					
					if(queryId == "") {
						//外部传入为空值的情况下，从参数中获取查询方法
						String queryIdTemp = map.get("queryId").toString();
						if(queryIdTemp != "") {
							dataList = queryService.queryByPagination(queryIdTemp, map, pageNum, pageSize);
						}
					}else {
						// 获取查询结果
						dataList = queryService.queryByPagination(queryId, map, pageNum, pageSize);
					}
					
					
				}
				result = new JsonMessage(true, dataList);
				
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				result = new JsonMessage(false, e.getMessage());
			}
			return result;
		}
		
		
	/**
	 * 请求报警处理率页面
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("/viewAlarmsProcessedPercent.action")
	public String viewAlarmsProcessedPercent(Model model) {
		model.addAttribute("message", "success");
		mylog.info("终端请求报警处理率页面...");
		return "alarmReport/viewAlarmsProcessedPercent";
	}
	
	
	@RequestMapping("/getAlarmsProcessedPercentPagedatas.action")
	public @ResponseBody JsonMessage getAlarmsProcessedPercentPagedatas(@RequestBody String jsonStr) {
//		String queryId = "alarmReport.selectAlarmsProcessedPercentPagedatasByDep";
		return getResultDatas("", jsonStr);
	}
		
}
